<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from "vue-router";
const router = useRouter()
import { login } from '@/api/login';
import { getInformationfilterlist } from '@/api/article';
import useUserStore from '@/store/modules/user';
const userStore = useUserStore()
getInformationfilterlist().then(res => {
	console.log(res)
})
const checked = ref(false);
const rememberPassword = ref(false);
const form = reactive({
	account: '',
	password: '',
	login_type: 1,
})
onMounted(() => {
	const savedCredentials = localStorage.getItem('credentials');
	if (savedCredentials) {
		const { account, password } = JSON.parse(savedCredentials);
		form.account = account;
		form.password = password;
		rememberPassword.value = true;
	}
})

const loginHandler = () => {
	if (!form.account) {
		ElMessage({
			showClose: true,
			message: '请输入账号！',
			type: 'warning',
		})
		return false;
	}
	if (!form.password) {
		ElMessage({
			showClose: true,
			message: '请输入密码！',
			type: 'warning',
		})
		return false;
	}
	if (!checked.value) {
		ElMessage({
			showClose: true,
			message: '请阅读并同意《服务条款》、《隐私条款》！',
			type: 'warning',
		})
		return false;
	}
	if (rememberPassword.value) {
		localStorage.setItem('credentials', JSON.stringify(form));
	} else {
		localStorage.removeItem('credentials');
	}
	let params = {
			"data": form,
		}
	login(params).then(res => {
		if(res.code===0){
			userStore.setUserInfo(res.data).then(res =>{
				router.push({
					path: '/'
				})
			})
		}
		console.log(res)
	})
}
</script>
<template>
	<div class="container">
		<div class="content">
			<div class="tab-nav">
				<div class="tab-item current">密码登录</div>
				<router-link to="./code">
					<div class="tab-item">短信登录</div>
				</router-link>
			</div>
			<div class="form-content">
				<div class="form-item">
					<label for="id1" class="accout"></label>
					<input id="id1" type="text" placeholder="请输入账号" v-model="form.account" @keyup.enter="loginHandler"/>
				</div>
				<div class="form-item">
					<label for="id3" class="password"></label>
					<input id="id3" type="password" placeholder="请输入密码" v-model="form.password" @keyup.enter="loginHandler" />
				</div>
				<div class="agree">
					<el-checkbox v-model="checked" label="我已阅读并同意《服务条款》、《隐私条款》" @keyup.enter="loginHandler"
					 />
				</div>
				<div class="agree" style="margin-top: 20px;">
					<el-checkbox v-model="rememberPassword" label="记住密码" />
				</div>
				<button type="button" class="submit" @click="loginHandler">登录</button>
				<div class="tip">
					<div class="hasAccout">
						<span>还没有账号？</span>
						<router-link to="/register">
							<span class="goLogin">立即注册</span>
						</router-link>
					</div>
					<div class="forget">
						<router-link to="/forget">
							<span class="forget-text">忘记密码</span>
						</router-link>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>
<style lang="scss" scoped>
.container {
	width: 100%;
	height: 100%;
	background-image: url(../../assets/images/loginBg.png);
	background-position: 0 0;
	background-repeat: no-repeat;
	background-size: 100% 100%;
	display: flex;
	align-items: center;
	justify-content: center;
}

.content {
	width: 736px;
	height: 688px;
	background: #FFFFFF;
	border-radius: 30px;
	overflow: hidden;
}

.form-content {
	margin-top: 60px;
}

.form-item {
	width: 480px;
	height: 60px;
	background: rgba(0, 0, 0, 0.05);
	border-radius: 5px;
	margin: 0 auto 40px auto;
	display: flex;
	align-items: center;
	padding: 0 22px;
	box-sizing: border-box;
	position: relative;
}

.form-item input {
	height: 25px;
	width: 400px;
	margin-left: 22px;
	border: none;
	background-color: transparent;
	padding-left: 20px;
	border-left: 1px solid rgba(0, 0, 0, 0.3);
	box-sizing: border-box;
	outline: none;
}

.get-code {
	position: absolute;
	right: 20px;
	top: 18px;
}

.accout {
	display: block;
	width: 17px;
	height: 25px;
	background: url(../../assets/icons/accout.png) no-repeat center center/100%;
}

.code {
	display: block;
	width: 22px;
	height: 25px;
	background: url(../../assets/icons/code.png) no-repeat center center/100%;
}

.password {
	display: block;
	width: 20px;
	height: 25px;
	background: url(../../assets/icons/password.png) no-repeat center center/100%;
}

.agree {
	width: 410px;
	height: 20px;
	margin: 0 auto;
	cursor: pointer;
	font-size: 18px;
	color: #333333;
}

.tab-nav {
	margin-top: 80px;
	display: flex;
	justify-content: center;

	a {
		text-decoration: none;
	}
}

.tab-item {
	font-family: MiSansVF_Medium;
	color: #333333;
	font-size: 28px;
	line-height: 37px;
	text-align: center;
	cursor: pointer;
}

.current {
	color: #45A1FF;
	margin-right: 100px;
}

.submit {
	display: block;
	width: 410px;
	height: 60px;
	line-height: 60px;
	background: #45A1FF;
	border-radius: 5px;
	font-family: MiSansVF_Medium;
	font-size: 22px;
	color: #FFFFFF;
	letter-spacing: 20px;
	text-align: center;
	border: none;
	margin: 30px auto 10px auto;
	cursor: pointer;
}

.tip {
	width: 410px;
	margin: 0 auto;
	display: flex;
	justify-content: space-between;

	a {
		text-decoration: none;
	}

	.goLogin {
		color: #45A1FF;
	}
}

.forget .forget-text {
	font-family: MiSansVF;
	font-size: 18px;
	color: #333333;
	line-height: 24px;
}

.get-code {
	font-family: MiSansVF;
	font-size: 18px;
	color: #45A1FF;
	border: none;
	cursor: pointer;
}

:deep(.el-checkbox__inner) {
	width: 17px;
	height: 17px;
	border-color: #979797;
	border-radius: 2px;
}

:deep(.el-checkbox__inner::after) {
	top: 2px;
	left: 5px;
}

:deep(.el-checkbox) {
	height: 20px;
}
</style>
